﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ParamSetCollection.General.Parameters.Normal;
using ParamSetCollection.General.Logs;

namespace MainController.Hardware.PLC.MEGMEET.Datas.Groups
{
    class DWGroup : ParamGroup
    {
        #region============================== 变量 ====================================
        //public override event OnRenewParamListEventHandler OnRenewParamListEvent;
        #endregion=====================================================================


        #region============================== 属性 ====================================
        #endregion=====================================================================


        #region============================= 内部函数 ================================
        private string CheckBoolValue(string val)
        {
            try
            {
                if (val.IndexOf('r') > 0)
                {
                    return "1";
                }
                else if (val.IndexOf('s') > 0)
                {
                    return "0";
                }
                else
                {
                    return val;
                }
            }
            catch (Exception)
            {

                return "0";
            }
        }


        #endregion====================================================================


        #region============================= 保护函数 ================================
        //修改data缓存值
        protected override void ChangeBuffer(string position,object value)
        {
            data[Convert.ToInt32(position)] = (ushort)((Int32)value & ushort.MaxValue);
            data[Convert.ToInt32(position) + 1] = (ushort)((Int32)value >> 16);

        }
        #endregion====================================================================


        #region============================= 公开函数 ================================
        public DWGroup(string paramType, string baseCode, Dictionary<string, string> paramDic, string cfgPath, ushort[] dataBuf)
            : base(paramType, baseCode, "DW.cfg",  paramDic, cfgPath, dataBuf)
        {

        }

        public override List<CommUnit> GetParamSetting(ref List<ParamUnit> paramList)
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            List<ParameterUnit> _paramList = new List<ParameterUnit>();
            foreach (ParamUnit item in paramList)
            {
                if (m_paramDic.ContainsKey(item.Code))
                {
                    string val = CheckBoolValue(item.Value);
                    if (m_paramDic[item.Code].Value == val)
                    {
                        continue;
                    }
                    //m_paramDic[item.Code].Value = item.Value;//修改成员值，产生修改信息

                    if (!item.DontRenewBufMark)
                    {
                        m_paramDic[item.Code].Value = val;
                        _paramList.Add((ParameterUnit)m_paramDic[item.Code].ShallowClone());
                        ChangeBuffer(m_paramDic[item.Code].Position,  Convert.ToInt32(m_paramDic[item.Code].Value));
                    }
                    else
                    {
                        ParameterUnit pu = (ParameterUnit)m_paramDic[item.Code].ShallowClone();
                        pu.Value = val;
                        _paramList.Add(pu);

                    }



                    //paramList.Remove(item);
                }
            }
            //获取写参数指令
            cmdList.AddRange(GetWriteDataCommands(null, _paramList));
            return cmdList;
        }

        public override List<CommUnit> InitCtx(object ctxData)
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            List<ParameterUnit> _paramList = new List<ParameterUnit>();

            Dictionary<string, int> intDic = ctxData as Dictionary<string, int>;
            if (intDic != null)
            {
                foreach (var key in intDic.Keys)
                {
                    if (m_paramDic.ContainsKey(key))
                    {
                        string val = CheckBoolValue(key).ToString();
                        if (m_paramDic[key].Value == val)
                        {
                            continue;
                        }

                        m_paramDic[key].Value = val;
                        _paramList.Add((ParameterUnit)m_paramDic[key].ShallowClone());

                        ChangeBuffer(m_paramDic[key].Position,  Convert.ToInt32(m_paramDic[key].Value));

                    }
                }
                //获取写参数指令
                cmdList.AddRange(GetWriteDataCommands(null, _paramList));
            }


            return cmdList;
        }

        public override List<CommUnit> GetInitCmdList()
        {
            //获取初始化指令
            List<CommUnit> cmdList = new List<CommUnit>();

            //获取写参数指令
            if (m_paramList.Count > 0)
            {
                cmdList.AddRange(GetWriteDataCommands(null, m_paramList));
            }
            return cmdList;
        }



        //更新参数，用于plc参数监控
        public override void InputNewDatas(ushort[] data)//
        {
            m_msgLock = true;
            Queue<ParamUnit> puQ = new Queue<ParamUnit>();
            foreach (string item in m_paramDic.Keys)
            {
                int pos = Convert.ToInt32(m_paramDic[item].Position);

                ushort lowWord = this.data[pos];
                ushort highWord = this.data[pos + 1];

                string v = ((Int32)((UInt32)(highWord << 16) + lowWord)).ToString();

                if (m_paramDic[item].Value != v)
                {
                    m_paramDic[item].Value = v;
                    ParamUnit pu = m_paramDic[item].TransferToRemoteUnit();
                    pu.Code = m_baseName + "." + pu.Code;
                    puQ.Enqueue(pu);
                }
            }
            //if (puQ.Count > 0 && OnRenewParamListEvent != null)
            //{
            //    OnRenewParamListEvent(puQ);
            //}

            puQ = null;
            m_msgLock = false;
        }



        //获取写参数指令
        public override List<CommUnit> GetWriteDataCommands(Dictionary<string, ushort> dataList = null, List<ParameterUnit> paramList = null)//
        {
            List<CommUnit> cmdList = new List<CommUnit>();
            if (paramList != null)
            {
                if (paramList.Count > 0)
                {
                    foreach (ParameterUnit item in paramList)
                    {
                        cmdList.Add(new CommUnit(CommAction.WriteMultReg, Convert.ToInt32(item.Position), new Int32[1] { Convert.ToInt32(CheckBoolValue(item.Value)) }));
                    }
                }
            }
            return cmdList;
        }
        #endregion====================================================================


        #region============================= 消息响应 ================================
        #endregion====================================================================
    }
}
